﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace GrayParrot.Web.Formly
{
    public static class FormlyExtensions
    {
        /// <summary>
        /// Sanitizes the dropdown item removing the characters from value of type string containig numerical values...
        /// </summary>
        /// <param name="item">The item.</param>
        /// <returns></returns>
        /// <remarks>
        /// The characters removed are: "string:" and "number:"
        /// </remarks>
        public static string SanitizeDropdownItem(this string item)
        {
            string retValue = item;

            retValue = retValue.Replace("string:", "").Replace("number:", "");

            return retValue;
        }

        /// <summary>
        /// Sanitizes the dropdown item removing the characters from value of type string containig numerical values...
        /// </summary>
        /// <param name="item">The item.</param>
        /// <returns></returns>
        /// <remarks>
        /// The characters removed are: "string:" and "number:"
        /// </remarks>
        public static string SanitizeDropdownItem(this JValue item)
        {
            string retValue = item.ToString();

            retValue = retValue.Replace("string:", "").Replace("number:", "");

            return retValue;
        }

        /// <summary>
        /// Sanitizes the dropdown item removing the characters from value of type string containig numerical values...
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="item">The item.</param>
        /// <returns>the value converted in the T type</returns>
        /// <remarks>
        /// The characters removed are: "string:" and "number:"
        /// </remarks>        
        public static T SanitizeDropdownItem<T>(this JValue item)
        {
            string retValue = item.ToString();

            retValue = (retValue == "?" ? "0" : retValue);
            retValue = retValue.Replace("string:", "").Replace("number:", "");

            return (T)Convert.ChangeType(retValue, typeof(T));
        }

        /// <summary>
        /// Sanitizes the dropdown item removing the characters from value of type string containig numerical values...
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="item">The item.</param>
        /// <returns>the value converted in the T type</returns>
        /// <remarks>
        /// The characters removed are: "string:" and "number:"
        /// </remarks>   
        public static T SanitizeDropdownItem<T>(this string item)
        {
            string retValue = item;

            retValue = (retValue == "?" ? "0" : retValue);
            retValue = retValue.Replace("string:", "").Replace("number:", "");

            return (T)Convert.ChangeType(retValue, typeof(T));
        }

        /// <summary>
        /// Converts to bool...
        /// </summary>
        /// <param name="item">The item.</param>
        /// <returns></returns>
        public static bool ConvertToBool(this string item)
        {
            bool retValue = false;

            retValue = (item.Equals("on", StringComparison.InvariantCultureIgnoreCase) ? true : false);

            return retValue;
        }

        /// <summary>
        /// Saves the image rapprsented by a image field...
        /// </summary>
        /// <param name="field">The image field</param>
        /// <returns></returns>
        public static bool Save(this ImageField field)
        {
            bool retValue = false;

            try
            {
                string filePath = string.Format(@"{0}\{1}", HttpContext.Current.Server.MapPath(field.FolderName), field.Filename);
                System.IO.File.WriteAllBytes(filePath, Convert.FromBase64String(field.Base64));

                retValue = true;
            }
            catch (Exception ex)
            {
            }

            return retValue;
        }
    }
}
